package com.atguigu.flink.charkoer13;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class FlinkSqlKafka {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.executeSql("CREATE TABLE sensor ( " +
                "  `id` string, " +
                "  `ts` BIGINT, " +
                "  `vc` int " +
                "   )with(" +
                "  'connector' = 'kafka', " +
                "  'topic' = 'topicA', " +
                "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                "  'properties.group.id' = 'gaowudi', " +
                "  'scan.startup.mode' = 'latest-offset', " +
                "  'format' = 'csv' " +
                ")");
        Table table = tEnv.sqlQuery("select * from sensor");

        tEnv.executeSql("CREATE TABLE s1( " +
                "  `id` string, " +
                "  `ts` BIGINT, " +
                "  `vc` int " +
                ")with( " +
                " 'connector' = 'kafka', " +
                "  'topic' = 's2', " +
                "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                "  'format' = 'json', " +
                "  'sink.partitioner' = 'round-robin' " +
                ")");

//        tEnv.executeSql("create table s1(" +
//                " id string, " +
//                " ts bigint, " +
//                " vc int" +
//                ")with(" +
//                " 'connector' = 'kafka', " +
//                "  'topic' = 's2', " +
//                "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
//                "  'format' = 'json', " +
//                "  'sink.partitioner' = 'round-robin' " +
//                ")");

        table.executeInsert("s1");
    }
}
